package com.myutils;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

public class RenameImages {

	public static void main(String[] args) throws IOException {
		String imgDir = "E:\\Images";
		rename1(imgDir);
	}
	
	public static void rename2(String imgDir) throws IOException{
		File[] files = FileUtil.getFilesOfDir(imgDir);
		
		Integer num = 1;
		
		for(File srcFile : files){
			String oldpath = srcFile.getCanonicalPath();
			String newName = Function.addLeftZero(num.toString(), 3);
			
			
			String ext = FileUtil.getExtension(oldpath);//获取后缀名
			String newpath = imgDir + "\\" + newName + "." + ext;
			
			File newFile = new File(newpath);
			if(!newFile.exists() && FileUtil.isMatchRegex(oldpath, "jpg|gif|jpeg|png|JPG|GIF|JPEG|PNG")){
				boolean isSucc = srcFile.renameTo(newFile);
				System.out.println(oldpath + "->" + newpath + " : " + isSucc);
				
				if(isSucc)
					num++;
			}
			
		}
	}
	
	public static void rename1(String imgDir) throws IOException{
		File[] files = FileUtil.getFilesOfDir(imgDir);
		for(File srcFile : files){
			String oldpath = srcFile.getCanonicalPath();
			
			long lastModifiedTime = srcFile.lastModified();
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(lastModifiedTime);
			Date date = cal.getTime();
			String newname = DateUtil.formatDate(date, "yyyyMMddHHmmss");
			
			String ext = FileUtil.getExtension(oldpath);//获取后缀名
			String newpath = imgDir + "\\" + newname + "." + ext;
			
			File newFile = new File(newpath);
			if(!newFile.exists()){
				System.out.println(oldpath + "->" + newpath + " : " + srcFile.renameTo(newFile));
			}
		}
	}
}
